package com.silence.whereeat.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 字符过滤器
 * Created by 伟阳 on 2015/9/14.
 */
public class EncodingFilter implements Filter {
    private FilterConfig filterConfig = null;
    private String encoding = null;

    public void destroy() {
        filterConfig = null;
        encoding = null;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        if (req.getCharacterEncoding() == null) {
            if (encoding != null) {
                req.setCharacterEncoding(encoding);
                resp.setCharacterEncoding(encoding);
                resp.setContentType("text/html;charset=UTF-8");
            }
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
        this.encoding = filterConfig.getInitParameter("encoding");
    }
}
